using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EM.Repository.utility;
using Telerik.WebControls;
using System.Data.SqlClient;
using System.Text;

namespace EM
{
    public partial class _Default : System.Web.UI.Page
    {
        public string APPROOT = "";

        DbHelper dsrv = new DbHelper();

        bool showAll
        {
            get
            {
                if (ViewState["showAll"] == null)
                    return false;
                else
                    return (bool)ViewState["showAll"];
            }
            set
            {
                if (ViewState["showAll"] == null)
                    ViewState.Add("showAll", value);
                else
                    ViewState["showAll"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            AppPathFinder AppPath = new AppPathFinder();
            APPROOT = AppPath.GetApplicationPath();

            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(5, Authentication.RedirectionURLTypes.LoginPage);

            GetCurrencyData();

            if (!IsPostBack)
            {
                GetBookMarkData();

            }
        }

        private void GetBookMarkData()
        {
            SqlCommand cmd = new SqlCommand();
            string sSQL = "";
            sSQL += @"SELECT BM.BOOKMARKID, P.PAGEID, BM.BOOKMARKNAME, 
                    CASE WHEN P.PAGEURL IS NULL THEN (SELECT P2.PAGEURL FROM TBLPAGE P2 WHERE P2.PAGEID = P.PARENTPAGEID) ELSE P.PAGEURL END AS PAGEURL
                    FROM TBLPAGE P, TBLBOOKMARK BM 
                    WHERE P.PAGEID = BM.PAGEID 
                    AND USERID = @USERID ";
            cmd.CommandText = sSQL;

            cmd.Parameters.Add("USERID", SqlDbType.Int).Value = HttpContext.Current.Session["USERID"].ToString();

            DataTable dt = dsrv.GetDataTable(cmd, "TBLBOOKMARK");
            rptBookMark.DataSource = dt;
            rptBookMark.DataBind();
        }

        private void GetCurrencyData()
        {

            string sSQL = "";
            sSQL += @" 
                        SELECT C.CURRENCYNAME, CONVERT(DECIMAL(18,4),dbo.fnConvertCurrencyAmount(C.CURRENCYID, 1, 1, GETDATE(), 'S')) AS CURENCYAMOUNT
                        FROM TBLCURRENCY C
                        WHERE dbo.fnConvertCurrencyAmount(C.CURRENCYID, 1, 1, GETDATE(), 'S') IS NOT NULL
                        AND CURRENCYID <> 1
                    ";
            DataTable dt = dsrv.GetDataTable(sSQL, "TBLBOOKMARK");
            rptCurrency.DataSource = dt;
            rptCurrency.DataBind();
        }

        protected void rptBookMark_ItemdDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRow dr = ((DataRowView)e.Item.DataItem).Row;

                HtmlAnchor hrBOOKMARKNAME = (HtmlAnchor)e.Item.FindControl("hrBOOKMARKNAME");
                hrBOOKMARKNAME.HRef = APPROOT + "/" + dr["PAGEURL"] + "?pgid=" + dr["PAGEID"];
            }
        }

        protected void rptBookMark_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string sSQL = "";
                sSQL += " DELETE FROM TBLBOOKMARK ";
                sSQL += " WHERE BOOKMARKID = @BOOKMARKID ";
                sSQL += " AND USERID = @USERID ";

                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = sSQL;
                cmd.Parameters.Add("BOOKMARKID", SqlDbType.Int).Value = e.CommandArgument;
                cmd.Parameters.Add("USERID", SqlDbType.Int).Value = HttpContext.Current.Session["USERID"].ToString();

                dsrv.ExecuteNonQuery(cmd);

                GetBookMarkData();
            }
        }


    }
}
